import BaseVenueProcessor from './BaseVenueProcessor.js';
import { getBuJiVenueSettings } from '../request.js';

/**
 * 埗击网球场地数据处理器
 */
export class BuJiProcessor extends BaseVenueProcessor {
  constructor() {
    super();
    this.venueName = '埗击网球';
  }

  /**
   * 获取埗击网球场地预约设置
   * @param {Object} params - 查询参数 { userId, projectUid, dateTime }
   * @returns {Promise} 请求Promise
   */
  async getVenueSettings(params = {}) {
    // 设置默认参数
    const defaultParams = {
      userId: 5860108,
      projectUid: "1745985425105631705",
      dateTime: "2025-08-20"
    };
    
    // 合并参数
    const requestData = { ...defaultParams, ...params };
    
    return await getBuJiVenueSettings(requestData);
  }

  /**
   * 验证数据是否有效
   * @param {Object} data - 原始场地数据
   * @returns {boolean} 数据是否有效
   */
  validateData(data) {
    // 处理两种不同的数据格式
    // 格式1: data包含result属性，result属性包含slots
    // 格式2: data直接包含slots
    const actualData = data.result || data;
    
    return actualData && 
           actualData.slots && 
           Array.isArray(actualData.slots) &&
           actualData.slots.length > 0;
  }

  /**
   * 按场地名称分组
   * @param {Object} data - 原始场地数据
   * @returns {Map} 场地Map
   */
  groupVenues(data) {
    const venueMap = new Map();
    
    // 处理两种不同的数据格式
    const actualData = data.result || data;
    
    // 从data中提取所有不同的场地名称
    const venueNames = [...new Set(actualData.slots.map(slot => slot.classRoomName))];
    
    // 为每个场地创建一个场地对象
    venueNames.forEach(venueName => {
      venueMap.set(venueName, {
        name: venueName,
        siteId: venueName, // 使用classRoomName作为siteId
        timeSlots: []
      });
    });
    
    return venueMap;
  }

  /**
   * 处理时间槽
   * @param {Map} venueMap - 场地Map
   * @param {Object} data - 原始场地数据
   */
  processTimeSlots(venueMap, data) {
    // 处理两种不同的数据格式
    const actualData = data.result || data;
    
    // 遍历所有时间槽
    actualData.slots.forEach(slot => {
      const venueName = slot.classRoomName;
      const venue = venueMap.get(venueName);
      
      if (venue) {
        const timeSlot = `${slot.beginDatetime.substring(11, 16)}-${slot.endDatetime.substring(11, 16)}`;
        
        venue.timeSlots.push({
          time: timeSlot,
          isFree: slot.status === 0, // 0表示可预约
          price: slot.cost || slot.realCost || 30,
          beginDatetime: slot.beginDatetime,
          endDatetime: slot.endDatetime
        });
      }
    });
  }

  /**
   * 转换为场地列表
   * @param {Map} venueMap - 场地Map
   * @returns {Array} 场地列表
   */
  convertToVenueList(venueMap) {
    return Array.from(venueMap.values()).map((venue, index) => {
      // 根据场地名称设置价格和设施
      let price = 30; // 默认价格
      let facilities = ['专业教练'];
      let address = "广东省东莞市高埗镇颐龙广场5楼";
      
      // 根据场地名称调整属性
      if (venue.name.includes('室内')) {
        facilities.push('室内场地');
        price = 40;
      } else if (venue.name.includes('户外')) {
        facilities.push('户外场地');
        price = 25;
      }
      
      return {
        id: 23000 + index, // 给埗击网球场地分配ID
        name: venue.name,
        address: address,
        latitude: 23.088525,
        longitude: 113.754210,
        description: '楼顶标准网球场',
        rating: 4.5,
        price: price,
        facilities: facilities,
        image: '',
        distance: 4.5,
        timeSlots: venue.timeSlots,
        dataSource: this.venueName
      };
    });
  }

  /**
   * 处理场地数据
   * @param {Object} data - 原始场地数据
   * @param {string} selectedDate - 选择的日期
   * @returns {Array} 处理后的场地列表
   */
  process(data, selectedDate) {
    this.selectedDate = selectedDate;
    console.log(data);
    
    // 验证数据
    if (!this.validateData(data)) {
      console.error('埗击网球数据验证失败');
      return [];
    }
    
    // 按场地名称分组
    const venueMap = this.groupVenues(data);
    
    // 处理时间槽
    this.processTimeSlots(venueMap, data);
    
    // 转换为场地列表
    return this.convertToVenueList(venueMap);
  }
}

export default BuJiProcessor;